package com.sgb.kjwl.view.ui.activity.power;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.graphics.drawable.Drawable;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityCommonList3Binding;
import com.sgb.kjwl.di.power.DaggerPowerComponent;
import com.sgb.kjwl.di.power.PowerModule;
import com.sgb.kjwl.model.entity.MaterialFilterEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.PowerEntity;
import com.sgb.kjwl.model.entity.PowerRequestEntity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.adapter.power.PowerNewAdapter;
import com.sgb.link.view.widget.PickCityPopWindow;
import com.sgb.kjwl.view.widget.PowerFliterPopWindow;
import com.sgb.kjwl.viewmodel.power.PowerListModel3;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

public class PowerListActivity3 extends AppActivity {

    @Inject
    PowerListModel3 model;
    private String id;
    private ActivityCommonList3Binding binding;
    private PowerNewAdapter adapter;
    private PowerFliterPopWindow popWindow;
    private PickCityPopWindow pickCityPopWindow;
    private PickCityPopWindow pickLevelPopWindow;
    private PickCityPopWindow pickRecordPopWindow;

    private List<MaterialFilterEntity> data;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPowerComponent.builder().appComponent(appComponent).powerModule(new PowerModule(this)).build().inject(this);
        id = getIntent().getStringExtra("id");
        model.filterJson.get().setCategoryNo(id);
        model.filterJson.get().setAreaNo("1");
        model.filterJson.get().setTypeNo("");
        model.filterJson.get().setLevelNo("");
        model.filterJson.get().setRecordNo("");
    }

    public static void start(Context context, String id, String title) {
        Intent intent = new Intent();
        intent.putExtra("id", id);
        intent.putExtra("title", title);
        intent.setClass(context, PowerListActivity3.class);
        context.startActivity(intent);
    }

    @Override
    protected boolean statusBarDark() {
        return true;
    }


    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_common_list3);
        binding.include.rightView.setVisibility(View.VISIBLE);
        binding.include.rightView.setText("筛选");
        Drawable drawable = getResources().getDrawable(R.drawable.shaixuan);
        drawable.setBounds(0, 0, 30, 30);
        binding.include.rightView.setCompoundDrawables(null, null, drawable, null);
        binding.include.rightView.setCompoundDrawablePadding(10);
        binding.include.titleView.setText(getIntent().getStringExtra("title"));
        binding.include.ivBack.setOnClickListener(this);
        binding.include.rightView.setOnClickListener(this);
        adapter = new PowerNewAdapter(getActivity(), null);
        binding.list.setLayoutManager(new LinearLayoutManager(this));
        binding.list.setAdapter(adapter);
        adapter.setOnItemClick(new AdapterOnItemClick<PowerEntity>() {
            @Override
            public void onItemClick(PowerEntity powerEntity, int position) {
               // PowerActivity.start(getActivity(), powerEntity.getNo());
                model.getUrl(powerEntity.getNo()+"");
            }
        });

        binding.list.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                model.getList(false);
            }

            @Override
            public void onLoadMore() {
                model.getList(true);
            }
        });

        popWindow = new PowerFliterPopWindow(this);
        popWindow.setBgView(binding.grayLayout);
        popWindow.setFilterResultClick(new PowerFliterPopWindow.FilterResultClick() {
            @Override
            public void result(PowerRequestEntity filterJson) {
                model.filterJson.get().setTypeNo(filterJson.getTypeNo());
                model.filterJson.get().setLevelNo(filterJson.getLevelNo());
                model.filterJson.get().setRecordNo(filterJson.getRecordNo());
                model.filterJson.get().setAreaNo(filterJson.getAreaNo());
                model.getList(false);

                binding.tvType.setText("".equals(popWindow.getName1()) ? "资质类型" : popWindow.getName1());
                pickCityPopWindow.setDefData(popWindow.getPosition1());
                if ("".equals(popWindow.getName1())) {
                    setTextViewChange(binding.tvType, binding.typeIv, R.mipmap.ic_down_arrow_n, R.color.color_666666);
                } else {
                    setTextViewChange(binding.tvType, binding.typeIv, R.mipmap.ic_up_arrow_blue, R.color.new_blue);
                }


                binding.tvLevel.setText("".equals(popWindow.getName2()) ? "资质等级" : popWindow.getName2());
                pickLevelPopWindow.setDefData(popWindow.getPosition2());
                if ("".equals(popWindow.getName2())) {
                    setTextViewChange(binding.tvLevel, binding.levelIv, R.mipmap.ic_down_arrow_n, R.color.color_666666);
                } else {
                    setTextViewChange(binding.tvLevel, binding.levelIv, R.mipmap.ic_up_arrow_blue, R.color.new_blue);
                }


                binding.tvRecord.setText("".equals(popWindow.getName3()) ? "备案单位" : popWindow.getName3());
                pickRecordPopWindow.setDefData(popWindow.getPosition3());
                if ("".equals(popWindow.getName3())) {
                    setTextViewChange(binding.tvRecord, binding.recordIv, R.mipmap.ic_down_arrow_n, R.color.color_666666);
                } else {
                    setTextViewChange(binding.tvRecord, binding.recordIv, R.mipmap.ic_up_arrow_blue, R.color.new_blue);
                }
            }
        });

        binding.typeLayout.setOnClickListener(this);
        binding.levelLayout.setOnClickListener(this);
        binding.recordLayout.setOnClickListener(this);
    }

    @Override
    protected void init() {
        //资质类型
        pickCityPopWindow = new PickCityPopWindow(this);
        pickCityPopWindow.setBgView(binding.grayLayout);
        pickCityPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                String id = (String) key1;
                String name = (String) key2;
                int position = (int) key3;
                model.filterJson.get().setTypeNo(id);
                if ("全部".equals(name)) {
                    if (!"资质类型".equals(binding.tvType.getText()))
                        model.getList(false);
                    binding.tvType.setText("资质类型");
                    setTextViewChange(binding.tvType, binding.typeIv, R.mipmap.ic_down_arrow_n, R.color.color_666666);
                } else {
                    binding.tvType.setText(name);
                    setTextViewChange(binding.tvType, binding.typeIv, R.mipmap.ic_up_arrow_blue, R.color.new_blue);
                    model.getList(false);
                    List<PickSelectEntity> pickSelectLevel = new ArrayList<>();
                    for (MaterialFilterEntity materialFilterEntity : data) {
                        if (id.equals(materialFilterEntity.getId())) {
                            for (MaterialFilterEntity level : materialFilterEntity.getDatas()) {
                                PickSelectEntity levelData = new PickSelectEntity();
                                levelData.setNo(level.getId());
                                levelData.setProjectscale(level.getName());
                                pickSelectLevel.add(levelData);
                            }
                            break;
                        }
                    }
                    pickLevelPopWindow.setData(pickSelectLevel);
                }

                model.filterJson.get().setLevelNo("");
                binding.tvLevel.setText("资质等级");
                setTextViewChange(binding.tvLevel, binding.levelIv, R.mipmap.ic_down_arrow_n, R.color.color_666666);

                model.filterJson.get().setRecordNo("");
                binding.tvRecord.setText("备案单位");
                setTextViewChange(binding.tvRecord, binding.recordIv, R.mipmap.ic_down_arrow_n, R.color.color_666666);

                popWindow.setDefData1(position, id,name);
            }


        });

        //资质等级
        pickLevelPopWindow = new PickCityPopWindow(this);
        pickLevelPopWindow.setBgView(binding.grayLayout);
        pickLevelPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                String id = (String) key1;
                String name = (String) key2;
                model.filterJson.get().setLevelNo(id);
                if ("全部".equals(name)) {
                    if (!"资质等级".equals(binding.tvLevel.getText()))
                        model.getList(false);
                    binding.tvLevel.setText("资质等级");
                    setTextViewChange(binding.tvLevel, binding.levelIv, R.mipmap.ic_down_arrow_n, R.color.color_666666);
                } else {
                    binding.tvLevel.setText(name);
                    setTextViewChange(binding.tvLevel, binding.levelIv, R.mipmap.ic_up_arrow_blue, R.color.new_blue);
                    model.getList(false);
                }
                popWindow.setDefData2((int) key3, id,name);

                model.filterJson.get().setRecordNo("");
                binding.tvRecord.setText("备案单位");
                setTextViewChange(binding.tvRecord, binding.recordIv, R.mipmap.ic_down_arrow_n, R.color.color_666666);
            }
        });
        //记录
        pickRecordPopWindow = new PickCityPopWindow(this);
        pickRecordPopWindow.setBgView(binding.grayLayout);
        pickRecordPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object key1, Object key2, Object key3) {
                String id = (String) key1;
                String name = (String) key2;
                model.filterJson.get().setRecordNo(id);
                if ("全部".equals(name)) {
                    if (!"备案单位".equals(binding.tvRecord.getText()))
                        model.getList(false);
                    binding.tvRecord.setText("备案单位");
                    setTextViewChange(binding.tvRecord, binding.recordIv, R.mipmap.ic_down_arrow_n, R.color.color_666666);
                } else {
                    binding.tvLevel.setText(name);
                    setTextViewChange(binding.tvRecord, binding.recordIv, R.mipmap.ic_up_arrow_blue, R.color.new_blue);
                    model.getList(false);
                }
                popWindow.setDefData3((int) key3, id,name);
            }
        });
        model.getList(false);
        model.getTypeFilter();
        model.getRecordFilter();
        model.getAreaFilter();
    }

    public void showList(List<PowerEntity> list, boolean isLoadMore) {
        binding.list.loadMoreComplete();
        binding.list.refreshComplete();
        if (isLoadMore) {
            adapter.addDatas(list);
        } else {
            if (list.size() == 0) {
                binding.emptyView.setVisibility(View.VISIBLE);
            } else {
                binding.emptyView.setVisibility(View.GONE);
            }
            adapter.setDatas(list);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;

            case R.id.right_view:
                popWindow.showPopwindow(binding.include.rightView);
                break;
            case R.id.type_layout:
                binding.typeIv.setImageResource(R.mipmap.ic_up_arrow_n);
                pickRecordPopWindow.dismiss();
                pickLevelPopWindow.dismiss();
                pickCityPopWindow.showPopwindow(binding.tvType);
                break;
            case R.id.level_layout:
                binding.levelIv.setImageResource(R.mipmap.ic_up_arrow_n);
                pickRecordPopWindow.dismiss();
                pickCityPopWindow.dismiss();
                pickLevelPopWindow.showPopwindow(binding.tvLevel);
                break;
            case R.id.record_layout:
                binding.recordIv.setImageResource(R.mipmap.ic_up_arrow_n);
                pickRecordPopWindow.showPopwindow(binding.tvRecord);
                pickCityPopWindow.dismiss();
                pickLevelPopWindow.dismiss();
                break;
        }
    }

    public void setTypeFilter(List<MaterialFilterEntity> data) {
        this.data = data;
        popWindow.setTypeLevelData(data);
        pickCityPopWindow.setData(formatData(data));
    }

    public void setRecordFilter(List<MaterialFilterEntity> data) {
        popWindow.setRecordData(data);
        pickRecordPopWindow.setData(formatData(data));

    }

    public void setAreaFilter(List<MaterialFilterEntity> data) {
        popWindow.setAreaData(data);
    }

    private void setTextViewChange(TextView textView, ImageView imageView, int img, int textColor) {

        imageView.setImageResource(img);
        textView.setTextColor(getResources().getColor(textColor));
    }

    private List<PickSelectEntity> formatData(List<MaterialFilterEntity> data) {
        List<PickSelectEntity> pickSelectEntityList = new ArrayList<>();
        for (MaterialFilterEntity materialFilterEntity : data) {
            PickSelectEntity pickSelectEntity = new PickSelectEntity();
            pickSelectEntity.setNo(materialFilterEntity.getId());
            pickSelectEntity.setProjectscale(materialFilterEntity.getName());
            pickSelectEntityList.add(pickSelectEntity);
        }
        return pickSelectEntityList;
    }


    public void getUrl(String url){
        WebViewX5HostActivity.start(PowerListActivity3.this,url);
    }
}
